// Created by liusc on 2020/8/5-9:24.
import { getDict } from "~/utils";
import { getInitObj, addObj } from "@/views/wy-budget/api/budgetinitiate.js";

export default {
    name: "initiate_add",
    created() {
        this.lists = getDict("budget_department");
    },
    data() {
        return {
            selectedArray: [],
            dialogVisible: false,
            isEdit: false,
            form: {
                applyTime: [],
                budgetId: "",
                budgetCode: "",
                applyName: "",
                applyDept: [],
                templateName: "",
                applyStart: "",
                applyEnd: "",
                budgetType: "11",
                remarks: ""
            },

            rules: {
                budgetCode: [
                    {
                        required: true,
                        message: "请输入预算编号",
                        trigger: "blur"
                    }
                ],
                applyName: [
                    {
                        required: true,
                        message: "请输入申请名称",
                        trigger: "blur"
                    },
                    {
                        min: 0,
                        max: 100,
                        message: "长度在 0 到 33 个字符",
                        trigger: "blur"
                    }
                ],
                applyDept: [
                    { required: true, message: "请选择部门", trigger: "change" }
                ],
                templateName: [
                    { required: true, message: "请输入模板", trigger: "blur" }
                ],
                applyTime: [
                    {
                        required: true,
                        message: "请选择申请时间",
                        trigger: "change "
                    }
                ],
                remarks: [
                    {
                        min: 0,
                        max: 100,
                        message: "长度在 0 到 100 个字符",
                        trigger: "blur"
                    }
                ]
            },
            //防止重复提交
            canSubmit: true
        };
    },

    mounted() {},

    methods: {
        selectAll1() {
            if (this.form.applyDept.length < this.lists.length) {
                this.form.applyDept = [];
                this.lists.map(item => {
                    this.form.applyDept.push(item.value);
                });
            } else {
                this.form.applyDept = [];
            }
        },
        changeSelect(val) {
            if (!val.includes("全选") && val.length === this.lists.length) {
                this.form.applyDept.unshift("全选");
            } else if (
                val.includes("全选") &&
                val.length - 1 < this.lists.length
            ) {
                this.form.applyDept = this.form.applyDept.filter(item => {
                    return item !== "全选";
                });
            }
        },
        removeTag(val) {
            if (val === "全选") {
                this.form.applyDept = [];
            }
        },
        daterangeChange(e) {
            let _this = this;
            _this.$nextTick(() => {
                _this.$set(_this.form, "applyTime", [e[0], e[1]]);
                _this.$forceUpdate();
            });
        },

        show() {
            const loading = this.$loading({
                lock: true,
                text: "数据处理中"
            });

            this.dialogVisible = true;
            this.isEdit = false;

            setTimeout(() => {
                this.$refs["add-form"].resetFields();

                getInitObj()
                    .then(resp => {
                        if (resp.data.data) {
                            this.form.budgetCode = resp.data.data.budgetCode;
                            this.form.templateName =
                                resp.data.data.templateName;
                        }
                        loading.close();
                    })
                    .catch(() => {
                        loading.close();
                    });
            }, 100);
        },

        commit() {
            const loading = this.$loading({
                lock: true,
                text: "数据处理中"
            });

            this.$refs["add-form"].validate(valid => {
                if (valid) {
                    var startt = this.dataFormat(
                        this.form.applyTime[0],
                        "yyyy-MM-dd"
                    );
                    var endt = this.dataFormat(
                        this.form.applyTime[1],
                        "yyyy-MM-dd"
                    );

                    let dept = this.form.applyDept;

                    this.form.id = new Date().getTime();
                    this.form.applyStart = startt;
                    this.form.applyEnd = endt;
                    this.form.applyDept = this.form.applyDept.join(",");
                    this.form.budgetType = "11";

                    let params = Object.assign({}, this.form);
                    this.form.applyDept = dept;

                    if (this.canSubmit) {
                        this.canSubmit = false;
                        addObj(params)
                            .then(resp => {
                                if (resp.data.data) {
                                    this.suc("数据提交成功");
                                    this.closeDialog();
                                    this.$emit("query");
                                }
                                loading.close();
                            })
                            .catch(() => {
                                loading.close();
                            })
                            .finally(() => {
                                this.canSubmit = true;
                            });
                    }

                    // this.$emit('addData', params)
                    // this.dialogVisible = false
                } else {
                    loading.close();
                }
            });
        },

        dataFormat(value, fmt) {
            let getDate = new Date(value);
            let o = {
                "M+": getDate.getMonth() + 1,
                "d+": getDate.getDate(),
                "h+": getDate.getHours(),
                "m+": getDate.getMinutes(),
                "s+": getDate.getSeconds(),
                "q+": Math.floor((getDate.getMonth() + 3) / 3),
                S: getDate.getMilliseconds()
            };
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(
                    RegExp.$1,
                    (getDate.getFullYear() + "").substr(4 - RegExp.$1.length)
                );
            }
            for (let k in o) {
                if (new RegExp("(" + k + ")").test(fmt)) {
                    fmt = fmt.replace(
                        RegExp.$1,
                        RegExp.$1.length === 1
                            ? o[k]
                            : ("00" + o[k]).substr(("" + o[k]).length)
                    );
                }
            }
            return fmt;
        },

        closeDialog() {
            (this.form = {
                applyTime: [],
                budgetId: "",
                budgetCode: "",
                applyName: "",
                applyDept: "",
                templateName: "",
                applyStart: "",
                applyEnd: "",
                remarks: ""
            }),
                (this.dialogVisible = false);
        }
    }
};
